package com.usfuture.techanly.proxy
{
	import com.usfuture.ISocket;
	import com.usfuture.IntelAddr;
	import com.usfuture.techanly.entity.Line;
	import com.usfuture.event.DataEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.external.ExternalInterface;
	import flash.net.Socket;
	import flash.sampler.NewObjectSample;
	
	public class LineProxy extends EventDispatcher implements ISocket
	{
		public static const NAME:String="lineProxy";
		
		private var _data:Array;
		
		/**
		 * 请求加载K线数据
		 * @default
		 */
		public static const LINE_DATA_LOAD:String="lineDataLoad";
		
		/**
		 * 请求加载先前的K线数据
		 * @default
		 */
		public static const LINE_PRE_DATA_LOAD:String="linePreDataLoad";
		
		/**
		 * 加载K线数据完成
		 * @default
		 */
		public static const LINE_DATA_LOAD_COMPLETE:String="lineDataLoadComplete";
		
		
		
		/**
		 * 加载数据类型
		 * @default
		 */
		public static var LINE_REQUEST_DATA_TYPE:String;//重新加载，加载前面数据，加载后面数据
		
		/**
		 * 加载先前的K线数据
		 * @default
		 */
		public static const LINE_REQUEST_PRE_DATA:String="pre";
		
		/**
		 * 加载后面的K线数据
		 * @default
		 */
		public static const LINE_REQUEST_AFTER_DATA:String="after";
		
		/**
		 * 加载后面的K线数据
		 * @default
		 */
		public static const LINE_REQUEST_RELOAD_DATA:String="reload";
		
		/**
		 * 一次加载的K线数量
		 * @default
		 */
		public static const LINE_DATA_LOAD_NUM:int=500;
		
		
		private var _socket:Socket;
		
		private var _dataStr:String="";
		
		public function connection():void
		{
			_socket=new Socket();
			_socket.addEventListener(Event.CLOSE, closehandler);
			_socket.addEventListener(Event.CONNECT, connectHandle);
			_socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandle);
			_socket.addEventListener(ProgressEvent.SOCKET_DATA, recieveHandle);
			_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityHandle);
			_socket.connect(IntelAddr.IP, IntelAddr.LOOKUP_PORT);
		}
		
		public function recieveHandle(event:ProgressEvent):void
		{
			if(_socket&&_socket.connected)
				_dataStr+=_socket.readUTFBytes(_socket.bytesAvailable);
			if (_dataStr.lastIndexOf('end') != -1)
			{
				var arr:Array=_dataStr.split("\n");
				for (var i:int=0; i < arr.length; i++)
				{
					var realData:Array=String(arr[i]).split(',');
					if (realData.length < 5)
						continue;
					var line:Line=new Line();
					line.timestamp=new Date(Number(realData[0]));
					line.highPrice=realData[1];
					line.lowPrice=realData[2];
					line.openPrice=realData[3];
					line.closePrice=realData[4];
					line.volume=realData[5];
					_data.push(line);
				}
				close();
				_data.reverse();
				dispatchEvent(new DataEvent(DataEvent.DATA_LOAD_COPMLEATE, _data));
			}
			
			
		}
		
		public function sendHandle(data:Object):void
		{
			
			var _lineType:String=data['lineType'];
			var maxTime:String=data['maxTime'];
			var redirect:String=data['redirect'];
			if(_socket){
				_socket.writeUTFBytes("line," + _lineType + ",A," + maxTime + ","+redirect+",end\r\n");
				_socket.flush();
			}
		}
		
		public function close():void
		{
			_dataStr='';
			if (_socket){
				_socket.removeEventListener(Event.CLOSE, closehandler);
				_socket.removeEventListener(Event.CONNECT, connectHandle);
				_socket.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandle);
				_socket.removeEventListener(ProgressEvent.SOCKET_DATA, recieveHandle);
				_socket.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityHandle);
				_socket.close();
			}
			_socket=null;
		}
		
		public function closehandler(evt:Event):void
		{
			trace("连接关闭");
			_socket=null;
			_dataStr='';
		}
		
		public function connectHandle(evt:Event):void
		{
			trace("连接建立");
			_dataStr="";
			_data=new Array;
		}
		
		public function ioErrorHandle(evt:IOErrorEvent):void
		{
			trace("io异常");
			_socket=null;
		}
		
		public function securityHandle(evt:SecurityErrorEvent):void
		{
			trace("安全异常");
			_socket=null;
		}
		
	}
}